<?php

class DashboardController extends Zend_Controller_Action {
	
	public function indexAction() {
		
	}
	
	public function uindexAction() {
		
		Nimblecms_Filesystem_Directory::rm_recursive(APPLICATION_PATH . '/../data/search-index2');
		
		$searchEngine = new Nimblecms_Search(APPLICATION_PATH . '/../data/search-index2');
		
		$countTerms = $searchEngine->updateIndex();
		
		$this->view->countTerms = $countTerms;
		/**try{
	        $index = Zend_Search_Lucene::open($this->_searchIndexPath);
	    } catch (Zend_Search_Lucene_Exception $e) {
	        echo "Ошибка:{$e->getMessage()}";
	    }
	    Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
	    $userQuery = Zend_Search_Lucene_Search_QueryParser::parse('*полиц*', 'utf-8');
	    $hits = $index->find($userQuery);;
	    
	    foreach ($hits as $hit) {
	    	echo $userQuery->highlightMatches($hit->content) . '<br />';
	    }*/
	}
	
	/**
	 * 
	 * Настройка сайта
	 */
	public function settingsAction() {
		$form = new Default_Form_Settings();
		$form->setAction($this->view->url(array(),'default_dashboard_settings'));
		
		$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
		
		if($setting) {
			$populate = array(
				'own_username' => $setting->own_username,
				'own_email' => $setting->own_email,
				'redirect' => $setting->send_email_feedback,
				'emails' => $setting->email_list,
				'is_smpt' => $setting->is_smtp,
				'smtp_host' => $setting->smtp_host,
				'smtp_login' => $setting->smtp_login,
				'smtp_password' => $setting->smtp_password
			);
			
			 $locales = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('locales');
			 
			 foreach ($locales as $lang=>$value) {
			 	$populate['description'][$lang] = $setting->Translation[$lang]->seo_description;
			 	$populate['keywords'][$lang] = $setting->Translation[$lang]->seo_keywords;
			 }
		}
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			if(!$setting) {
				$setting = new Default_Model_Settings();
			}
			
			foreach ($values['description'] as $lang=>$value) {
				$setting->Translation[$lang]->seo_description = $value;
			}
			foreach ($values['keywords'] as $lang=>$value) {
				$setting->Translation[$lang]->seo_keywords = $value;
			}
			$setting->own_username = $values['own_username'];
			$setting->own_email = $values['own_email'];
			$setting->send_email_feedback = $values['redirect'];
			$setting->email_list = $values['emails'];
			$setting->is_smtp = $values['is_smtp'];
			$setting->smtp_host = $values['smtp_host'];
			$setting->smtp_login = $values['smtp_login'];
			$setting->smtp_password = $values['smtp_password'];
			$setting->save();
			
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Settings has been save successfully!'));
			return $this->_redirect($this->view->url(array(),'default_dashboard_settings'));
		} else {
			if($setting) {
				$form->populate($populate);
			}
		}
		
		$this->view->form = $form;
	}
}
